/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dipl.algorithm.utility.convert;

import dipl.algorithm.math.primitive.Point2d;
import dipl.algorithm.math.fp.primitive.Point2df;
import dipl.algorithm.math.utility.ApfloatUtils;
import org.apfloat.Apfloat;

/**
 * Methods to convert from Apfloat to standard types
 */
public class ApfloatConversion {

	/**
	 * Convert arbitrary precision point array to double-precision point-array
	 * @param p
	 * @return
	 */
	public static Point2df ConvertPoint( Point2d p ) {
		if( p == null )
			return null;
		return new Point2df( p.x.doubleValue(), p.y.doubleValue() );
	}

	/**
	 * Convert double precision points to arbitrary-precision points
	 * @param p
	 * @return
	 */
	public static Point2d ConvertPoint( Point2df p ) {
		if( p == null )
			return null;
		Apfloat x = new Apfloat( p.x ).toRadix( ApfloatUtils.radix );
		Apfloat y = new Apfloat( p.y ).toRadix( ApfloatUtils.radix );
		return new Point2d( x, y );
	}

	/**
	 * Convert arbitrary precision point array to double-precision point-array
	 * @param p
	 * @return
	 */
	public static Point2df[] ConvertPoints( Point2d[] p ) {
		if( p == null || p.length == 0 )
			return null;
		
		Point2df[] res = new Point2df[ p.length];
		for( int i = 0; i < p.length; i++ ) {
			res[i] = new Point2df( p[i].x.doubleValue(), p[i].y.doubleValue() );
		}
		return res;
	}

	/**
	 * Convert double precision points to arbitrary-precision points
	 * @param p
	 * @return
	 */
	public static Point2d[] ConvertPoints( Point2df[] p ) {
		if( p == null || p.length == 0 )
			return null;

		Point2d[] res = new Point2d[ p.length];
		for( int i = 0; i < p.length; i++ ) {
			Apfloat x = new Apfloat( p[i].x ).toRadix( ApfloatUtils.radix );
			Apfloat y = new Apfloat( p[i].y ).toRadix( ApfloatUtils.radix );
			res[i] = new Point2d( x, y );
		}
		return res;
	}
}
